Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 9, 2025

When running brew bundle dump --global for the first time, Homebrew was ignoring XDG_CONFIG_HOME and falling back to ~/.Brewfile instead of creating the Brewfile in the XDG-compliant location ${XDG_CONFIG_HOME}/homebrew/Brewfile.

The issue was in the path resolution logic in Library/Homebrew/bundle/brewfile.rb which checked for the existence of the Brewfile before deciding to use the XDG path:

if user_config_home && File.exist?("#{user_config_home}/Brewfile")

This meant that for initial dumps where no Brewfile existed yet, it would fall back to the legacy path even when XDG_CONFIG_HOME was properly set.

Changes Made

  1. Enhanced path resolution logic in brewfile.rb to intelligently choose between XDG and legacy paths while maintaining full backwards compatibility:

    • When XDG is not set: uses legacy path (unchanged behavior)
    • When XDG file exists: uses XDG path (unchanged)
    • When legacy file exists but XDG doesn't: uses legacy path (maintains backwards compatibility)
    • When neither exists: uses XDG path (fixes the initial creation issue)
  2. Added directory creation in dumper.rb using Pathname#mkpath to ensure the XDG directory structure is created automatically when needed

  3. Added comprehensive test coverage for all backwards compatibility scenarios including when HOMEBREW_USER_CONFIG_HOME is set but neither file exists yet

  4. Refined XDG detection logic based on reviewer feedback to properly distinguish between XDG paths (${XDG_CONFIG_HOME}/homebrew) and legacy paths (${HOME}/.homebrew) by checking if HOMEBREW_USER_CONFIG_HOME ends with /homebrew

  5. Implemented lazy evaluation to avoid expensive operations unless necessary and restructured conditional logic to reduce duplication

Behavior After Fix

  • XDG_CONFIG_HOME=/custom/path brew bundle dump --global → creates /custom/path/homebrew/Brewfile (only when no existing files)
  • Default behavior (no XDG_CONFIG_HOME) → still creates ~/.Brewfile as expected (unchanged)
  • Existing legacy ~/.Brewfile files are preserved and used even when XDG is configured (backwards compatible)
  • Existing Brewfiles in XDG locations continue to work unchanged
  • HOMEBREW_BUNDLE_FILE_GLOBAL override continues to take precedence

Backwards Compatibility

This implementation ensures that no existing workflows are disrupted. Users with existing ~/.Brewfile files will continue to use them even after setting XDG_CONFIG_HOME, maintaining full backwards compatibility while still enabling XDG compliance for new installations.

Fixes #20628.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Remove File.exist? check in brewfile.rb to use XDG path even when Brewfile doesn't exist yet
- Add directory creation in dumper.rb to ensure parent directories exist before writing
- Add test case for XDG behavior when Brewfile doesn't exist initially

Co-authored-by: MikeMcQuaid <[email protected]>
@Copilot Copilot AI changed the title [WIP] Bug: initial brew bundle dump --global ignores XDG_CONFIG_HOME Fix brew bundle dump --global to respect XDG_CONFIG_HOME for initial Brewfile creation Sep 9, 2025
@Copilot Copilot AI requested a review from MikeMcQuaid September 9, 2025 07:49
Copilot finished work on behalf of MikeMcQuaid September 9, 2025 07:49
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This approach might make sense but I'm lacking confidence that this won't change the default behaviour for most users.

- Restore backwards compatibility by checking both XDG and legacy Brewfile paths
- Use existing file when available, only use XDG path for new file creation
- Replace FileUtils.mkdir_p with Pathname#mkpath as requested
- Add comprehensive test coverage for all scenarios

Co-authored-by: MikeMcQuaid <[email protected]>
Copy link
Contributor Author

Copilot AI commented Sep 9, 2025

@copilot This approach might make sense but I'm lacking confidence that this won't change the default behaviour for most users.

I've addressed the backwards compatibility concerns with a more conservative approach. The logic now:

  1. Preserves all existing behavior when XDG_* is unset
  2. Respects existing legacy files even when XDG is configured
  3. Only uses XDG path for new file creation when no existing file is found

This maintains full backwards compatibility while still fixing the initial brew bundle dump --global issue with XDG_CONFIG_HOME. (commit 3a35890)

@Copilot Copilot AI changed the title Fix brew bundle dump --global to respect XDG_CONFIG_HOME for initial Brewfile creation Fix brew bundle dump --global to respect XDG_CONFIG_HOME for initial Brewfile creation with backwards compatibility Sep 9, 2025
@Copilot Copilot AI requested a review from MikeMcQuaid September 9, 2025 12:18
Copilot finished work on behalf of MikeMcQuaid September 9, 2025 12:18
@Copilot Copilot AI requested a review from MikeMcQuaid September 9, 2025 12:59
Copilot finished work on behalf of MikeMcQuaid September 9, 2025 12:59
@rrotter
Copy link
Contributor

rrotter commented Sep 12, 2025

Do we really care how HOMEBREW_USER_CONFIG_HOME was populated (from XDG_CONFIG vs the default of ~/.homebrew, or some future method that does not yet exist)? If we do, we should not try to guess that here, but rather change the logic when HOMEBREW_USER_CONFIG_HOME is set.

(alternate PR incoming…)

@ZhongRuoyu ZhongRuoyu deleted the copilot/fix-brew-bundle-xdg-config-issue branch September 15, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: initial brew bundle dump --global ignores XDG_CONFIG_HOME
3 participants